package net.villonanny;

import java.util.Date;


public class TimeWhenRunnable {
	public static TimeWhenRunnable NEVER = new TimeWhenRunnable(false);
	public static TimeWhenRunnable NOW = new TimeWhenRunnable(true);
	private boolean enabled = true;
	private Date timeWhenRunnable = null;
	private boolean sharp = false;

	public TimeWhenRunnable(boolean enabled) {
		this.enabled = enabled;
		if (enabled) {
			timeWhenRunnable = new Date();
		} else {
			timeWhenRunnable = Util.getIncrediblyLongTime();
		}
	}

	public TimeWhenRunnable(long millisecond) {
		this( new Date(millisecond) );
	}

	public TimeWhenRunnable(long millisecond, boolean sharp) {
		this( new Date(millisecond), sharp );
	}
	
	/**
	 * @param timeWhenRunnable a valid date, null for "now" or Util.getIncrediblyLongTime() to disable
	 */
	public TimeWhenRunnable(Date timeWhenRunnable) {
		if (timeWhenRunnable == null) {
			timeWhenRunnable = new Date(); // now
		} else if (timeWhenRunnable.equals(Util.getIncrediblyLongTime())) {
			this.enabled = false;
		}
		this.timeWhenRunnable = timeWhenRunnable;
	}
	
	public TimeWhenRunnable(Date timeWhenRunnable, boolean sharp) {
		this.timeWhenRunnable = timeWhenRunnable;
		this.sharp = sharp;
	}
	
	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public Date getTimeWhenRunnable() {
		return timeWhenRunnable;
	}

	public void setTimeWhenRunnable(Date timeWhenRunnable) {
		this.timeWhenRunnable = timeWhenRunnable;
	}

	public boolean isSharp() {
		return sharp;
	}

	public void setSharp(boolean precision) {
		this.sharp = precision;
	}

	public boolean before(TimeWhenRunnable whenRunnable) {
		if ((timeWhenRunnable != null) && (whenRunnable != null)) {
			return timeWhenRunnable.before(whenRunnable.getTimeWhenRunnable());
		}
		return false;
	}

	public boolean before(Date date) {
		if ((timeWhenRunnable != null) && (date != null)) {
			return timeWhenRunnable.before(date);
		}
		return false;
	}

	public boolean after(TimeWhenRunnable whenRunnable) {
		if ((timeWhenRunnable != null) && (whenRunnable != null)) {
			return timeWhenRunnable.after(whenRunnable.getTimeWhenRunnable());
		}
		return false;
	}

	public boolean after(Date date) {
		if ((timeWhenRunnable != null) && (date != null)) {
			return timeWhenRunnable.after(date);
		}
		return false;
	}

	public long getTime() {
		if (timeWhenRunnable == null)
			return Util.getIncrediblyLongTime().getTime();
		
		return timeWhenRunnable.getTime();
	}
	
}
